{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "guilty-reality",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mGraph\u001b[39m"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Graph {\n",
    "    class Node {\n",
    "        var connectedNodes:List[Graph#Node] = Nil\n",
    "        def connectTo(node:Graph#Node){\n",
    "            if(! connectedNodes.exists(node.equals)){\n",
    "                connectedNodes = node :: connectedNodes\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    var nodes: List[Node] = Nil\n",
    "    def newNode:Node = {\n",
    "        val res = new Node\n",
    "        nodes = res :: nodes\n",
    "        res\n",
    "    }\n",
    "}\n",
    "//如果不用Graph#前缀，就不能连接两个图形。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eleven-cleaners",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mgraph1\u001b[39m: \u001b[32mGraph\u001b[39m = ammonite.$sess.cmd2$Helper$Graph@f57c0eb\r\n",
       "\u001b[36mnode1\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@24e8ef13\r\n",
       "\u001b[36mnode2\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@6fa52d80\r\n",
       "\u001b[36mnode3\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@5ec4f807"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val graph1:Graph = new Graph\n",
    "val node1:graph1.Node = graph1.newNode\n",
    "val node2:graph1.Node = graph1.newNode\n",
    "val node3:graph1.Node = graph1.newNode\n",
    "\n",
    "node1.connectTo(node2)\n",
    "node3.connectTo(node1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "promotional-sister",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mgraph1\u001b[39m: \u001b[32mGraph\u001b[39m = ammonite.$sess.cmd2$Helper$Graph@c4fc568\r\n",
       "\u001b[36mnode1\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@c973fcc\r\n",
       "\u001b[36mnode2\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@1c3d1082\r\n",
       "\u001b[36mgraph2\u001b[39m: \u001b[32mGraph\u001b[39m = ammonite.$sess.cmd2$Helper$Graph@102ff1b4\r\n",
       "\u001b[36mnode3\u001b[39m: \u001b[32mgraph2\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd2$Helper$Graph$Node@62b9125c"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val graph1: Graph = new Graph\n",
    "val node1: graph1.Node = graph1.newNode\n",
    "val node2: graph1.Node = graph1.newNode\n",
    "node1.connectTo(node2)      // legal\n",
    "val graph2: Graph = new Graph\n",
    "val node3: graph2.Node = graph2.newNode\n",
    "node1.connectTo(node3)      // illegal!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exciting-james",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
